import math
math.log(8,2)
def getlog2(x):
    return math.log(x,2)
def getI(x1,x2):
    p1=x1/(x1+x2)
    p2=x2/(x1+x2)
    return -p1*getlog2(p1)-p2*getlog2(p2)
def gete(x1,x2,x3,x4):
    p1=(x1+x2)/(x1+x2+x3+x4)
    p2=(x3+x4)/(x1+x2+x3+x4)
    return p1*getI(x1,x2)+p2*getI(x3,x4)
def getgrain(x1,x2,x3,x4,x5,x6):
    return getI(x5,x6)-gete(x1,x2,x3,x4)
'''
x1:特征+，结果+
x2:特征+，结果-
x3:特征-，结果+
x4:特征-，结果-
x5:结果总+
x6:结果总-
'''
'''
import pandas as pda
#读取数据
#对数据格式进行整理
from sklearn.tree import DecisionTreeClassifier as DTC
dtc=DTC(criterion="entropy")
#dtc.fit(需要训练的数据)
#可视化决策树-Graphviz
'''
import pandas as pda
#读取数据
fname="D:/Python35/data/lesson.csv"
dataf=pda.read_csv(fname,encoding="gbk")
x=dataf.iloc[:,1:5].as_matrix()
y=dataf.iloc[:,5].as_matrix()
for i in range(0,len(x)):
    for j in range(0,len(x[i])):
        thisdata=x[i][j]
        if(thisdata=="是" or thisdata=="多"):
            x[i][j]=int(1)
        else:
            x[i][j]=int(0)
for i in range(0,len(y)):
    thisdata=y[i]
    if(thisdata=="高"):
        y[i]=int(1)
    else:
        y[i]=int(0)
#中转---转为数据框，再由数据框转数组
xf=pda.DataFrame(x)
yf=pda.DataFrame(y)
x2=xf.as_matrix().astype(int)
y2=yf.as_matrix().astype(int)
from sklearn.tree import DecisionTreeClassifier as DTC
dtc=DTC(criterion="entropy")
dtc.fit(x2,y2)
x1=input("是否实战:")
x2=input("课时数多少")
x3=input("是否促销")
x4=input("是否提供配套资料")
isdo=[[int(x1),int(x2),int(x3),int(x4)]]
rst=dtc.predict(isdo)
if(rst==1):
    print("这门课程销量预测会高")
else:
    print("这门课程销量预测会低")
#可视化决策树
from sklearn.tree import export_graphviz
from sklearn.externals.six import StringIO
with open("D:/我的教学/Python/CSDN-Python机器学习/tree.dot","w") as file:
    export_graphviz(dtc,feature_names=["shizhan","keshishu","chuxiao","peitao"],out_file=file)
